<?php
/**
 * Created by PhpStorm.
 * User: Kyle
 * Date: 2019/9/2
 * Time: 15:02
 */

namespace app\backend\controller;


use app\base\controller\AdminBaseController;
use app\common\model\Admin;
use think\facade\Request;
use think\facade\Session;

class Login extends AdminBaseController
{
    public function index(){
        if(Session::get('userId')){
            return redirect('/admin');
        }
        return $this->fetch();
    }

    public function deal(){
        $name = $this->request->post('username','');
        $password = $this->request->post('password','');

        if(empty($name) || empty($password)){wapAjaxReturn(400,'信息不完整');}

        $info = Admin::where('username',$name)->find();
        if(!$info){
            wapAjaxReturn(400,'当前用户不存在');
        }
        if(md5($password.'.+!.'.$info['salt']) != $info['password']){
            wapAjaxReturn(400,'密码错误');
        }


        Session::set('userId',$info['id']);
        Session::set('username',$info['username']);
        Session::set('roles_id',$info['roles_id']);
        Session::set('is_super',$info['is_super']);

        wapAjaxReturn(0,'','','/admin');
    }

    public function logout(){
        Session::set('userId',0);
        Session::set('username','');
        //跳转登陆
        return redirect('/admin/login');
    }

    public function changePwd(){
        return $this->fetch();
    }

    public function changePwdPost(){
        $password = Request::param('password');
        $user = Admin::where('id',Session::get('userId'))->find();
        $user->password = md5($password.'.+!.'.$user->salt );
        $user->save();
        wapAjaxReturn(0,'修改成功');
    }
}